<template>
  <div>
    <!-- 调货单提报 -->
    <TopFilter :list="topFilterList" :cols="3" @filterChange="changeHandle" @onCollapse="collapseHandle" :labelWidth="110"></TopFilter>

    <FilterTable
      ref="table"
      columnsRef="zjafOrderSubmission"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      :onRowSelectChange="selectChangeHandle"
      :onColumnsChange="columns => (this.columns = columns)"
    >
      <template slot="controls" slot-scope="props">
        <multiuse-button size="small" type="primary" @click="addHandle(1)">新 增</multiuse-button>
        <multiuse-button size="small" type="primary" :disabled="isCan" :click="delHandle">批量删除</multiuse-button>
        <!-- <multiuse-button size="small" type="primary" @click="addHandle(2)">导 出</multiuse-button> -->
      </template>
    </FilterTable>

    <Drawer :visible.sync="addVisible" :title="'订单信息'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }" width="72%">
      <addModel @close="addcloseHandler" />
    </Drawer>
    <Drawer :visible.sync="emmitVisible" :title="'订单信息'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }" width="72%">
      <emitModel :rowData="formData" @close="emitcloseHandler" />
    </Drawer>
    <Drawer :visible.sync="detailVisible" :title="'订单信息'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }" width="72%">
      <detailModel :rowData="formData" @close="detailcloseHandler" />
    </Drawer>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { getList, exportList, getNotPage, deleteOrderById, subOrderById, getDealerUser, delByIds } from '@/api/dealerSale/OrderManagement/afOrderSubmission/index.js';
import addModel from './addModel';
import emitModel from './emitModel';
import detailModel from './detailModel';
import { exportExcelFile } from '@/utils';
export default {
  name: 'TemplateDistribution',
  data() {
    this.BaseTable = null;
    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      fetchParams: {
        searchType: 1,
        saveType: 'DLR',
        majorType: 1
      },
      fetchApi: getList,
      formData: {},
      emmitVisible: false,
      issearch: false,
      detailVisible: false,
      addVisible: false,
      isCan: true,
      arrIds: []
    };
  },
  mixins: [dictionary],
  components: {
    addModel,
    emitModel,
    detailModel
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  created() {
    this.getUser();
  },
  methods: {
    createdFrom() {
      return [
        {
          type: 'SELECT',
          label: '订单状态',
          fieldName: 'orderStatus',
          placeholder: '请选择',
          itemList: [
            { text: '待提交', value: 60431001 },
            { text: '审批驳回', value: 60431002 }
          ]
        },
        {
          type: 'INPUT',
          label: '订货单编号',
          fieldName: 'orderNo',
          placeholder: '请输入'
        },
        {
          type: 'SELECT',
          label: '订单类型',
          fieldName: 'orderType',
          placeholder: '请选择',
          itemList: [{ text: '调货订单', value: 60061007 }],
          change: p => {
            this.getNotPage(p);
          }
        },
        {
          type: 'SELECT',
          label: '订单类型细分',
          fieldName: 'orderDetailType',
          placeholder: '请选择',
          itemList: []
        },
        {
          type: 'RANGE_DATE',
          label: '创建日期',
          fieldName: 'createdAtstar|createdAtend',
          placeholder: '请选择',
          initialValue: [],
          dateType: 'exactdaterange'
        }
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 160,
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>
                <multiuse-button size="mini" type="text" onClick={() => this.editHandle(props.row, 1)} disabled={props.row.orderStatus != 60431001 && props.row.orderStatus != 60431002}>
                  编辑
                </multiuse-button>
                <el-divider direction="vertical" />
                <multiuse-button size="mini" type="text" onClick={() => this.editHandle(props.row, 2)}>
                  详情
                </multiuse-button>
                <el-divider direction="vertical" />
                <multiuse-button size="mini" type="text" onClick={() => this.editHandle(props.row, 3)} disabled={props.row.orderStatus != 60431001}>
                  提报
                </multiuse-button>
              </div>
            );
          }
        },

        {
          title: '订货单编号',
          dataIndex: 'orderNo',
          sorter: true,
          filter: true,
          filterType: 'input',
          align: 'center',
          width: 200
        },
        {
          title: 'App订单号',
          dataIndex: 'appOrderNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '订单类型',
          dataIndex: 'orderType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: [{ text: '调货订单', value: 60061007 }],
          render: p => {
            return <span>{this.createDictText(p.row.orderType, '6006')}</span>;
          }
        },
        {
          title: '订单细分类型',
          dataIndex: 'orderDetailTypeName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '订单状态',
          dataIndex: 'orderStatus',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: [
            { text: '待提交', value: 60431001 },
            { text: '审批驳回', value: 60431002 }
          ],
          render: p => {
            return <span>{this.createDictText(p.row.orderStatus, '6043')}</span>;
          }
        },

        {
          title: '订单年月',
          dataIndex: 'orderYearMonth',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '运输方式',
          dataIndex: 'transportType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6004'),
          render: p => {
            return <span>{this.createDictText(p.row.transportType, '6004')}</span>;
          }
        },
        {
          title: '提报数量',
          dataIndex: 'orderTotalNum',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '创建人',
          dataIndex: 'createdBy',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: [],
          editType: 'select',
          editItems: []
        },
        {
          title: '创建日期',
          dataIndex: 'createdAt',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          dateFormat: 'yyyy-MM-DD'
        }
      ];
    },
    selectChangeHandle(row) {
      if (row && row.length > 0) {
        this.isCan = false;
        this.arrIds = [];
        row.map(x => {
          this.arrIds.push(x.id);
        });
        return;
      }
      this.isCan = true;
    },
    // 批量删除
    async delHandle() {
      const res = await delByIds(this.arrIds);
      if (res.resultCode == 200) {
        this.$notify.success({ title: '提示信息', message: '删除成功' });
        this.fetchParams = Object.assign({}, this.fetchParams);
      }
    },
    async getNotPage(orderType) {
      let list = await getNotPage({ orderType: orderType });
      this.topFilterList.map(x => {
        if (x.fieldName == 'orderDetailType') {
          x.itemList = list.data.map(x => ({ text: x.subName, value: x.subId }));
        }
      });
    },
    seacherFun() {
      this.issearch = true;
    },
    addHandle(type) {
      if (type == 1) {
        this.addVisible = true;
      } else {
        this.$confirm('是否将Excel表格保存至本地文件夹?', '提示', {
          confirmButtonText: '下载',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            let json = this.fetchParams;
            exportList(json)
              .then(response => {
                exportExcelFile(response.data, '订货单查询');
                this.exportLoading = false;
              })
              .catch(err => {
                console.log(err);
              });
          })
          .catch(() => {
            console.log('456');
            this.$message({
              type: 'info',
              message: '已取消'
            });
          });
      }
    },
    changeHandle(params) {
      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    async editHandle(data, type) {
      this.formData = data;
      if (type == 1) {
        this.emmitVisible = true;
      } else if (type == 2) {
        this.detailVisible = true;
      } else if (type == 3) {
        await this.$confirm('是否进行提报?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(async () => {
            let res = await subOrderById({ id: data.id });
            if (res.resultCode == 200) {
              this.$notify({
                title: '成功',
                message: '提交成功',
                type: 'success'
              });
              this.fetchParams = Object.assign({}, this.fetchParams);
            }
          })
          .catch(() => {});
      } else {
        await this.$confirm('是否确定取消?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(async () => {
            let res = await deleteOrderById({ orderId: data.orderId });
            if (res.resultCode == 200) {
              this.$notify({
                title: '成功',
                message: '取消成功',
                type: 'success'
              });
              this.fetchParams = Object.assign({}, this.fetchParams);
            }
          })
          .catch(() => {});
      }
    },
    emitcloseHandler(val) {
      this.emmitVisible = false;
      if (val) {
        this.fetchParams = Object.assign({}, this.fetchParams);
      }
    },
    addcloseHandler(val) {
      this.addVisible = false;
      if (val) {
        this.fetchParams = Object.assign({}, this.fetchParams);
      }
    },
    detailcloseHandler() {
      this.detailVisible = false;
    },
    async getUser() {
      let res = await getDealerUser();
      let empList = res.data.map(x => ({ text: x.employeeName, value: x.userId + '' }));
      empList = empList.concat([{ text: '', value: '-1' }]);
      this.columns.find(item => item.dataIndex === 'createdBy').editItems = empList; //操作姓名
      this.columns.find(item => item.dataIndex === 'createdBy').filterItems = empList;
    }
  }
};
</script>
